package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.IncomeCertificate;
import com.amsp.oa.oademo.service.IncomeCertificateService;
import com.amsp.oa.oademo.vo.IncomeCertificateVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收入证明Controller
 */
@RestController
@RequestMapping("/api/income-certificate")
@Api(tags = "收入证明管理")
public class IncomeCertificateController {

    @Autowired
    private IncomeCertificateService incomeCertificateService;

    /**
     * 获取收入证明列表
     */
    @GetMapping("/list")
    @ApiOperation("获取收入证明列表")
    public Map<String, Object> getList(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("员工姓名") @RequestParam(required = false) String name,
            @ApiParam("部门") @RequestParam(required = false) String department) {

        // 创建分页对象
        Page<IncomeCertificate> page = new Page<>(current, size);

        // 调用服务获取分页数据
        Page<IncomeCertificateVO> resultPage = incomeCertificateService.getList(page, name, department);

        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "获取列表成功");

        Map<String, Object> data = new HashMap<>();
        data.put("records", resultPage.getRecords());
        data.put("total", resultPage.getTotal());
        data.put("size", resultPage.getSize());
        data.put("current", resultPage.getCurrent());
        data.put("pages", resultPage.getPages());

        result.put("data", data);

        return result;
    }

    /**
     * 获取收入证明详情
     */
    @GetMapping("/{id}")
    @ApiOperation("获取收入证明详情")
    public Map<String, Object> getDetail(@ApiParam("收入证明ID") @PathVariable Long id) {
        // 调用服务获取详情
        IncomeCertificateVO incomeCertificateVO = incomeCertificateService.getDetail(id);

        // 构建返回结果
        Map<String, Object> result = new HashMap<>();

        if (incomeCertificateVO != null) {
            result.put("code", 200);
            result.put("message", "获取详情成功");
            result.put("data", incomeCertificateVO);
        } else {
            result.put("code", 404);
            result.put("message", "收入证明不存在");
            result.put("data", null);
        }

        return result;
    }

    /**
     * 保存或更新收入证明
     */
    @PostMapping("/save")
    @ApiOperation("保存或更新收入证明")
    public Map<String, Object> saveOrUpdate(@ApiParam("收入证明信息") @RequestBody IncomeCertificateVO incomeCertificateVO) {
        // 调用服务保存或更新
        boolean success = incomeCertificateService.saveOrUpdate(incomeCertificateVO);

        // 构建返回结果
        Map<String, Object> result = new HashMap<>();

        if (success) {
            result.put("code", 200);
            result.put("message", "保存成功");
            result.put("data", true);
        } else {
            result.put("code", 500);
            result.put("message", "保存失败");
            result.put("data", false);
        }

        return result;
    }

    /**
     * 批量保存收入证明
     */
    @PostMapping("/batch-save")
    @ApiOperation("批量保存收入证明")
    public Map<String, Object> batchSave(
            @ApiParam("收入证明信息列表") @RequestBody List<IncomeCertificateVO> incomeCertificateVOList) {
        // 调用服务批量保存
        boolean success = incomeCertificateService.batchSave(incomeCertificateVOList);

        // 构建返回结果
        Map<String, Object> result = new HashMap<>();

        if (success) {
            result.put("code", 200);
            result.put("message", "批量保存成功");
            result.put("data", true);
        } else {
            result.put("code", 500);
            result.put("message", "批量保存失败");
            result.put("data", false);
        }

        return result;
    }

    /**
     * 删除收入证明
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除收入证明")
    public Map<String, Object> delete(@ApiParam("收入证明ID") @PathVariable Long id) {
        // 调用服务删除
        boolean success = incomeCertificateService.delete(id);

        // 构建返回结果
        Map<String, Object> result = new HashMap<>();

        if (success) {
            result.put("code", 200);
            result.put("message", "删除成功");
            result.put("data", true);
        } else {
            result.put("code", 500);
            result.put("message", "删除失败");
            result.put("data", false);
        }

        return result;
    }

    /**
     * 批量删除收入证明
     */
    @DeleteMapping("/batch")
    @ApiOperation("批量删除收入证明")
    public Map<String, Object> batchDelete(@ApiParam("收入证明ID列表") @RequestBody List<Long> ids) {
        // 调用服务批量删除
        boolean success = incomeCertificateService.batchDelete(ids);

        // 构建返回结果
        Map<String, Object> result = new HashMap<>();

        if (success) {
            result.put("code", 200);
            result.put("message", "批量删除成功");
            result.put("data", true);
        } else {
            result.put("code", 500);
            result.put("message", "批量删除失败");
            result.put("data", false);
        }

        return result;
    }
}